package com.daigoudao.web.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * 购物车
 * 
 * @author zheng
 * 
 */
public class Cart implements Serializable {

	private static final long serialVersionUID = -5388988705305884520L;

	/**
	 * 按经纪人保存购物项
	 */

	private Map<Integer, Map<Integer, CartItem>> items = new HashMap<Integer, Map<Integer, CartItem>>();

	public void add(CanBuyModel product, int number) {
		Map<Integer, CartItem> subItems = null;
		if (items.containsKey(product.getUser().getUserid())) {
			subItems = items.get(product.getUser().getUserid());
		} else {
			subItems = new HashMap<Integer, CartItem>();
			items.put(product.getUser().getUserid(), subItems);
		}

		CartItem item = null;
		if (subItems.containsKey(product.getCanId())) {
			item = subItems.get(product.getCanId());
			item.addNumber(number);
		} else {
			item = new CartItem(product, number);
			subItems.put(product.getCanId(), item);
		}
	}

	public void setNumber(int brokerId, int productId, int number) {
		if (items.containsKey(brokerId)) {
			Map<Integer, CartItem> subItems = items.get(brokerId);
			if (subItems.containsKey(productId)) {
				subItems.get(productId).setNumber(number);
			}
		}
	}

	public void delItem(int brokerId, int productId) {
		if (items.containsKey(brokerId)) {
			items.get(brokerId).remove(productId);
		}
	}

	public void delItems(int brokerId) {
		items.remove(brokerId);
	}

	public void delItems(int brokerId, int[] productIds) {
		if (items.containsKey(brokerId)) {
			Map<Integer, CartItem> subItems = items.get(brokerId);
			for (int productId : productIds) {
				subItems.remove(productId);
			}
			if (subItems.size() == 0) {
				items.remove(brokerId);
			}
		}
	}

	public Map<Integer, CartItem> getItems(int brokerId) {
		return items.get(brokerId);
	}

	/**
	 * 根据经纪人ID及产品ID列表获取相应的购物项
	 * 
	 * @param brokerId
	 * @param productIds
	 * @return
	 */
	public List<CartItem> getItems(int brokerId, int[] productIds) {
		List<CartItem> list = new ArrayList<CartItem>();
		Map<Integer, CartItem> subItems = getItems(brokerId);
		for (int productId : productIds) {
			if (subItems.containsKey(productId)) {
				list.add(subItems.get(productId));
			}
		}
		return list;
	}

	public int getCount() {
		int num = 0;
		for (Map<Integer, CartItem> subItems : items.values()) {
			num += subItems.size();
		}
		return num;
	}

	public double getTotal() {
		double total = 0;
		for (Map<Integer, CartItem> subItems : items.values()) {
			for (CartItem item : subItems.values()) {
				total += item.getSubtotal();
			}
		}
		return BigDecimal.valueOf(total).setScale(2, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
	}

	public Set<Integer> getBrokerIds() {
		return new TreeSet<Integer>(items.keySet());
	}

	public Map<Integer, Map<Integer, CartItem>> getItems() {
		return items;
	}
}
